package com.lizx.generator;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.lizx.common.BaseEntity;

import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 快速生成
 * </p>
 *
 * @author lanjerry
 * @since 2021-09-16
 */
public class FastAutoGeneratorTest {

    /**
     * 表名
     */
    private static final List<String> TABLES = Arrays.asList("tb_role","tb_user","tb_user_role");

    /**
     * 数据源配置
     */
    private static final DataSourceConfig.Builder DATA_SOURCE_CONFIG = new DataSourceConfig
        .Builder("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC", "root", "123456");


    private void globalConfigBuilder(GlobalConfig.Builder builder) {
        builder.fileOverride()//覆盖已经生成文件
                .outputDir("F:\\IdeaWorksplace\\mybatis-generator\\src\\main\\java")//输出目录
                .author("01408642")
                .enableSwagger()//开启Swagger
                .commentDate("yyyy-MM-dd");
    }

    private void packageConfigBuilder(PackageConfig.Builder builder) {
        builder
                .parent("com")
                .moduleName("lizx")
                .entity("entity")
                .service("service")
                .serviceImpl("service.impl")
                .mapper("mapper")
                .xml("mapper.xml")
                .controller("controller")
                .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "F:\\IdeaWorksplace\\mybatis-generator\\src\\main\\java\\com\\lizx\\mapperXml"));
    }

    private void strategyConfigBuilder(StrategyConfig.Builder builder) {
        builder
                .addInclude(TABLES)//表名称
                .addTablePrefix("tb_")//过滤表名前缀
                .serviceBuilder()//service配置策略
                .formatServiceFileName("%sService")//%s适配，根据表名替换
                .formatServiceImplFileName("%sServiceImpl")//同上
                .entityBuilder()//entity策略
                .superClass(BaseEntity.class)
                .enableLombok()//开启lombok
                //.logicDeleteColumnName("deleted")//逻辑删除字段
                //.enableTableFieldAnnotation()//属性加上说明注释
                .controllerBuilder()//策略Controller
                .formatFileName("%sController")//同上
                .enableRestStyle()//开启restcontroller
                .mapperBuilder()//mapper策略
                .superClass(BaseMapper.class)//集成父类
                .formatMapperFileName("%sMapper")//同上
                .enableMapperAnnotation()//@mapper开启
                .formatXmlFileName("%sMapper");

    }

    public void execute() {
        FastAutoGenerator.create(DATA_SOURCE_CONFIG)
                .globalConfig(this::globalConfigBuilder)
                .packageConfig(this::packageConfigBuilder)
                .strategyConfig(this::strategyConfigBuilder)
                .templateEngine(new FreemarkerTemplateEngine())
                .execute();
    }

    public static void main(String[] args) throws SQLException {
        FastAutoGeneratorTest generatorTest = new FastAutoGeneratorTest();
        generatorTest.execute();
    }
}